#!/bin/bash
if [ $# -ne 2 ]; then
    echo "usage: `basename $0` jobname jobport"
    exit 1
fi
JOB_BASE_NAME=$1
server_port=$2

[ -d /data/prog/project/${JOB_BASE_NAME} ] || mkdir -p  /data/prog/project/${JOB_BASE_NAME} 
project_jar_path=/data/prog/project/${JOB_BASE_NAME}/${JOB_BASE_NAME}.jar
update_jar_path=/data/update/${JOB_BASE_NAME}/${JOB_BASE_NAME}.jar

[ -f ${project_jar_path} ] && { 
    echo "检测到旧的jar包，开始替换"
    backup_path=/data/backup/$(date +%F-%H-%M-%S)
    mkdir -p ${backup_path}
    /bin/cp  -f ${project_jar_path} ${backup_path}/
    /bin/cp  -f ${update_jar_path} ${project_jar_path}
} || {
    /bin/cp  -f ${update_jar_path} ${project_jar_path}
}

jps |  grep ${JOB_BASE_NAME}.jar && {
    echo "开始kill ${JOB_BASE_NAME}.jar"
    kill -9  $(ps -ef | grep ${JOB_BASE_NAME}.jar | grep -v grep  |awk '{print $2}')
}
sleep 1
echo "开始部署 ${JOB_BASE_NAME} "
[ -d /data/logs/project/${JOB_BASE_NAME} ]  || mkdir -p /data/logs/project/${JOB_BASE_NAME}/
sleep 1
cd /data/prog/project/${JOB_BASE_NAME}/
nohup /data/prog/jdk1.8.0_271/bin/java \
    -server -Xms500m -Xmx500m  \
    -jar /data/prog/project/${JOB_BASE_NAME}/${JOB_BASE_NAME}.jar \
    --server.port=${server_port} --spring.profiles.active=dev \
    --spring.cloud.nacos.config.server-addr=192.168.50.203:8848 \
    --spring.cloud.nacos.config.username=nacos \
    --spring.cloud.nacos.config.password=nacos \
    --spring.cloud.nacos.discovery.server-addr=192.168.50.203:8848 \
    --spring.cloud.nacos.discovery.username=nacos \
    --spring.cloud.nacos.discovery.password=nacos >> /data/logs/project/${JOB_BASE_NAME}/${JOB_BASE_NAME}-console.log 2>&1 & 
echo "部署成功！"